Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Yes, I know that this sounds complicated. Let’s take a look at the syntax of the PaintPicture method
    object.PaintPicture source, x1, y1, w1, h1, x2, y2, w2, h2, opcode
where the arguments are as follows: • object—The name of the Picture Box, Form, or Printer object where the picture is to be placed. This argument is optional. If it’s omitted, the form with the focus is assumed. • source—The source of the graphic. Must be the Picture property of a Form, Picture, or Picture Box object. • x1, y1—Single-precision values indicating the destination coordinates—in other words, the location on the destination object where the top-left corner of the image is to be drawn. The ScaleMode property of the object determines the unit of measure used. • w1, h1—Single-precision values indicating the destination width and height of the picture, using units specified by the ScaleMode property of the destination object. If the destination width and/or height is larger or smaller than the source width (w2) or height (h2), the picture is stretched or compressed to fit. These arguments are optional; if they’re omitted, the source width (w1) and height (h1) are used with no stretching or compression. • x2, y2—Single-precision values indicating the source coordinates of the region in the source object that is to be copied (in units specified by the source object’s ScaleMode property). These arguments are optional; if they’re omitted, 0 is assumed (indicating the top-left corner of the source image). • w2, h2—Single-precision values indicating the width and height of the region within the source that is to be copied (in units specified by the source object’s ScaleMode property). These arguments are optional; if they’re omitted, the entire source width and height are used. • opcode—A type Long value that defines the bitwise operation performed between the pixels of the source picture and the pixels of any existing image on the destination (explained shortly). This argument, which is optional, is useful only with bitmaps. If the argument is omitted, the source is copied onto the destination, replacing anything that is there. Think of the source picture as being printed on a rubber sheet. You can cut out any rectangular part of the picture you want, stretch it vertically and/or horizontally, and stick it down anywhere on the destination picture. Sound useful? You bet it is. The operation of PaintPicture is illustrated in Figure 12.6. The opcode argument can be a bit confusing—with good reason. How many ways can you copy a picture from one location to another? Well, you would be surprised—dozens, at least. Here’s how it works: In the destination object, where the copied picture is going to be placed, an image already exists. A bitmap picture may be displayed there already, but even if the destination is blank, something is there—perhaps just white pixels. Over the extent of the copied region, therefore, each pixel in the copied image has a corresponding pixel at the same location in the destination. How will these pixels be combined? This is what the opcode argument determines. Figure 12.6 The PaintPicture method’s arguments. The simplest—and perhaps most common—operation is to have each new pixel replace the original pixel. The opcode argument for this is the predefined constant vbSrcCopy. Two other useful opcode arguments are vbBlackness and vbWhiteness, which turn the destination rectangle all black or all white. This really isn’t a copy of the source picture, of course; you still have to specify a source in the PaintPicture statement. Other opcode arguments perform a variety of logical combinations of the source picture with the destination picture. You can use them to create certain types of visual effects, but I won’t go into them here. You can explore the topic further in the Visual Basic Help system. TIP: Hiding The Picture Box Don’t forget that you can set a Picture Box’s Visible property to False, so it does not display on screen. Even while hidden, it is still a valid source for the PaintPicture method, and you can still load pictures into it by setting its Picture property or by using the LoadPicture statement. The best way to get a feel for how PaintPicture works is to see it in action. The example program PAINTPIC demonstrates three common uses for this statement: enlarging a picture, shrinking a picture, and painting areas with black or white. To start the project, place two Picture Box controls on a form. For the first one (Picture1), set the AutoSize property to True, then load the picture GUARD.JPG (provided on the companion CD-ROM) into its Picture property. All of the properties for Picture2 should be left at their default values. Change the form’s Caption property to Picture Magnifier. The program requires three constants, placed in the general declarations section of the form’s code. Two of the constants specify the vertical and horizontal magnification, expressed as the portion of the original picture that will be blown up. I used 0.3 for both the vertical and horizontal values; you can experiment with other values if you like. The third constant is the opcode vbWhiteness. Unfortunately, this is not predefined by Visual Basic—we have do it ourselves. The declarations code is shown here:
    Option Explicit
    Const vbWhiteness = &HFF0062
    Const WIDE = 0.3
    Const HIGH = 0.3
Adjusting the form and control sizes is accomplished in the Form_Load event procedure. Picture1 will already be the size of the loaded picture, because we set its AutoSize property to True. The following code makes the form size a bit taller than and a bit more than twice as wide as this Picture Box. Then it makes Picture2 the same size as Picture1 and positions them both on the form. The code is shown in Listing 12.4. Listing 12.4 The Picture Magnifier’s Form_Load procedure.
    Private Sub Form_Load()

    Form1.Width = 2.1 * Picture1.Width
    Form1.Height = 1.1 * Picture1.Height

    Picture1.Left = 0
    Picture1.Top = 0

    Picture2.Width = Picture1.Width
    Picture2.Height = Picture1.Height

    Picture2.Top = 0
    Picture2.Left = Form1.ScaleWidth - Picture2.Width

    End Sub
The real action will be triggered by the user clicking on the source picture. A left-button click will trigger the display (in the second Picture Box) of a magnified portion of the picture centered on the location where the mouse was clicked. A right-button click will display nine small copies of the entire picture, each surrounded by a black and white border. Let’s deal with detecting the click first. While the Click event procedure might seem like the way to go, a quick examination reveals that it provides no way to tell which button was clicked or where the mouse pointer was located. Clearly, we need something else. Scanning through the list of events that the Picture Box control supports, you will notice MouseDown and MouseUp events. The MouseUp event procedure declaration looks like this:
    Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As
    Single)
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.